/**
 * @file    boot_param.c
 * @brief   boot参数
 * @author  Liuwei
 * @version 1.0.1
 * @date    2023-05-11
 * 
 * @copyright Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version   <th>Author   <th>Description
 * <tr><td>2023-05-11 <td>1.0.1     <td>Liuwei   <td>首次创建
 * </table>
 */

/* includes ------------------------------------------------------------------*/
#include "boot_param.h"
#include "drv_flash.h"
#include "cfg_prj.h"
#include "If_com.h"
#include "cfg_prj.h"
#include "var_mng.h"
#include "iap_boot.h"
/* macro/define --------------------------------------------------------------*/
#define  BOOT_WRITE_SIZE   (BOOT_PAR_SIZE * 4)
/* typedef -------------------------------------------------------------------*/

/* local functions -----------------------------------------------------------*/
/* static variables ----------------------------------------------------------*/
static uint32_t boot_par_buf[BOOT_PAR_SIZE] = {0};
static uint32_t calc_md5_crc(uint32_t src);
/* global variables ----------------------------------------------------------*/
static uint32_t mcu_uid = 0;
static uint32_t calc_uid = 0;
/** ------------------------------------------------------------------------- *
  *                             Global function
 ** ------------------------------------------------------------------------- */
void boot_par_init(void)
{
#ifdef USE_IAP_BOOT
	drv_flash_init();
	
    drv_flash_read_data(BOOT_PAR_ADDR , (uint8_t *)boot_par_buf , BOOT_WRITE_SIZE);
	
	drv_flash_read_data(BOOT_MD5_ADDR , (uint8_t *)&mcu_uid , 4);
	
	calc_uid = FMC_ReadUID(0);
	calc_uid = calc_md5_crc(mcu_uid);
	
	if(calc_uid != mcu_uid)
	{
		;
	}
//    
//    if(mcu_uid == 0xFFFFFFFF)
//    {
//        mcu_uid = FMC_ReadUID(0);
//        mcu_uid = calc_md5_crc(mcu_uid);
//        drv_flash_write_data(BOOT_MD5_ADDR , (uint8_t *)&mcu_uid ,4);
//    }
	
	drv_flash_close();
#endif
}

void boot_param_set_par(uint8_t par_id , uint32_t par_val)
{
#ifdef USE_IAP_BOOT
    if(par_id >= BOOT_PAR_SIZE)
		return ;
	
	boot_par_buf[par_id] = par_val;
	
	drv_flash_init();
	
	drv_flash_erase_area(BOOT_PAR_ADDR , BOOT_PAR_ADDR + FMC_FLASH_PAGE_SIZE);
	
	drv_flash_write_data(BOOT_PAR_ADDR , (uint8_t *)boot_par_buf , BOOT_WRITE_SIZE);
	
	drv_flash_close();
#endif
}

uint32_t boot_param_read_par(uint8_t par_id)
{
    if(par_id >= BOOT_PAR_SIZE)
		return 0;
	
	return boot_par_buf[par_id];
}

uint32_t boot_param_read_md5(uint8_t par_id)
{
	uint32_t md5_code = 0;
	
    if(par_id >= BOOT_PAR_SIZE)
		return 0;
	
	drv_flash_read_data(BOOT_MD5_ADDR , (uint8_t *)&md5_code , 4);
	
	return md5_code;
}

void boot_param_app_write(uint8_t src_id)
{
#ifdef USE_IAP_BOOT
	drv_flash_init();
	
	drv_flash_read_data(BOOT_PAR_ADDR , (uint8_t *)boot_par_buf , BOOT_WRITE_SIZE);
	
	boot_par_buf[BP_ID_UPGRADE_FLAG] = src_id;
	boot_par_buf[BP_ID_PARALLEL_RATE] = COM_PARALLEL_RATE;
	boot_par_buf[BP_ID_PCS_RATE] = COM_PCS_BOOT_RATE;
	boot_par_buf[BP_ID_LCD_RATE] = COM_BLUE_BOOT_RATE;
	boot_par_buf[BP_ID_BLUE_RATE] = COM_BLUE_BOOT_RATE;
	boot_par_buf[BP_ID_RS232_RATE] = COM_PCS_BOOT_RATE;
	boot_par_buf[BP_ID_GPS_RATE] = COM_BLUE_BOOT_RATE;
	
	boot_par_buf[BP_ID_APP_SIZE] = 0;
	boot_par_buf[BP_ID_APP_CRC] = 0;

    drv_flash_erase_area(BOOT_PAR_ADDR , BOOT_PAR_ADDR + 1);

    drv_flash_write_data(BOOT_PAR_ADDR , (uint8_t *)boot_par_buf , BOOT_WRITE_SIZE);

	drv_flash_close();
#endif
}

void boot_param_write_reset(void)
{
#ifdef USE_IAP_BOOT	
	drv_flash_init();
	
	drv_flash_read_data(BOOT_PAR_ADDR , (uint8_t *)boot_par_buf , BOOT_WRITE_SIZE);
	
	boot_par_buf[BP_ID_RESET_FLAG] = USE_CMD_RESET;
	
    drv_flash_erase_area(BOOT_PAR_ADDR , BOOT_PAR_ADDR + 1);

    drv_flash_write_data(BOOT_PAR_ADDR , (uint8_t *)boot_par_buf , BOOT_WRITE_SIZE);
		
	drv_flash_close();
#endif
}

void boot_clear_upgrade_flag(void)
{
    if(boot_par_buf[BP_ID_UPGRADE_FLAG] != 0)
    {
		drv_flash_init();
		
		boot_par_buf[BP_ID_UPGRADE_FLAG] = 0;
		
        drv_flash_erase_area(BOOT_PAR_ADDR , BOOT_PAR_ADDR + 1);

        drv_flash_write_data(BOOT_PAR_ADDR , (uint8_t *)boot_par_buf , BOOT_WRITE_SIZE);
		
		drv_flash_close();
    }
}

uint8_t boot_param_is_upgrade(void)
{
	if(0 == boot_par_buf[BP_ID_UPGRADE_FLAG])
	{
		return FALSE;
	}
	else if((BOOT_UPGRADE_EMS == boot_par_buf[BP_ID_UPGRADE_FLAG])
		||(BOOT_UPGRADE_PRALLEL == boot_par_buf[BP_ID_UPGRADE_FLAG])
		||(BOOT_UPGRADE_PCS == boot_par_buf[BP_ID_UPGRADE_FLAG])
		||(BOOT_UPGRADE_LCD == boot_par_buf[BP_ID_UPGRADE_FLAG])
		||(BOOT_UPGRADE_BLUE == boot_par_buf[BP_ID_UPGRADE_FLAG])
		||(BOOT_UPGRADE_CAN1 == boot_par_buf[BP_ID_UPGRADE_FLAG])
		||(BOOT_UPGRADE_CAN2 == boot_par_buf[BP_ID_UPGRADE_FLAG]))
	{
		return TRUE;
	}
	else 
	{
		return FALSE;
	}
}

void boot_clear_reset_flag(void)
{
	if(boot_par_buf[BP_ID_RESET_FLAG] == USE_CMD_RESET)
    {
		drv_flash_init();
		
		boot_par_buf[BP_ID_RESET_FLAG] = 0;
		
        drv_flash_erase_area(BOOT_PAR_ADDR , BOOT_PAR_ADDR + 1);

        drv_flash_write_data(BOOT_PAR_ADDR , (uint8_t *)boot_par_buf , BOOT_WRITE_SIZE);
		
		drv_flash_close();
    }
}

/** ------------------------------------------------------------------------- *
  *                              Local function
 ** ------------------------------------------------------------------------- */
static uint32_t calc_md5_crc(uint32_t src)
{
    return src = (src + 0x1A2B3C4D) ^ src;
} 

/********** Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD *********/
